/**
 * related to detail.ui
 * 
 * @Author : chenglm
 * @Timestamp : 2016-07-16
 */
var page = sm("do_Page");
var app = sm("do_App");
var do_External = sm("do_External");
var nf = sm("do_Notification");
var hashdata = mm("do_HashData");
var orderUtil = require("url/order");
var qqHttp = require("helper/http");
var openHelper = require("helper/open");
var helperOrder = require("helper/order");
var do_LinearLayout_item = ui("do_LinearLayout_item");
var do_LinearLayout_1 = ui("do_LinearLayout_1");

var labelOrderStatus = ui("labelOrderStatus");
var iconBg = ui("iconBg");
var do_Label_consignee = ui("do_Label_consignee");
var do_Label_phone = ui("do_Label_phone");
var do_Label_address = ui("do_Label_address");
var do_Label_buyer = ui("do_Label_buyer");
var do_Label_total = ui("do_Label_total");

var do_Label_orderNo = ui("do_Label_orderNo");
var do_Label_strNewAt = ui("do_Label_strNewAt");
var do_Label_payStatusAt = ui("do_Label_payStatusAt");
var do_Label_orderStatusAt = ui("do_Label_orderStatusAt");

var do_Button_sended = ui("do_Button_sended");
var layoutBtn = ui("layoutBtn");
var button_phone = ui("button_phone");
var label_remark = ui("label_remark");
var label_payTypeStr = ui("label_payTypeStr");

//andriod返回键
page.on("back", function() {
	app.closePage();
});

var headerAout = ui("do_ALayout_header");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
//左上角-返回
var headerLeft = ui(addheader+".headerLeft");
headerLeft.on("touch", function() {
	app.closePage();
});
var header = ui("header");
header.bindData(hashdata);
hashdata.addData({
    "labelTitle" : "订单详情"
})
header.refreshData();

//页面装载完成后，开始初始化工作
page.on("loaded", function(){
	var orderNo = page.getData().orderNo;
	getData(orderNo);
});
var order;
function getData(orderNo) {
	deviceone.print(orderNo);
	qqHttp.post(orderUtil.url.base.sellDetail, {orderNo:orderNo}, function(data) {
		//deviceone.print(JSON.stringify(data));
		order = data.order;
		var delivery = data.delivery;
		var itemList = data.itemList;
		labelOrderStatus.text = order.orderStatusText;
		label_remark.text = order.remark;
		do_Button_sended.visible=false;
		layoutBtn.visible=false;
		if (order.orderStatus == 3 || order.orderStatus == 3) {
			iconBg.source="source://image/order/close.png";
		} else if (order.orderStatusText == "待付款") {
			iconBg.source="source://image/order/wait.png";
		} else if (order.orderStatusText == "交易完成") {
			iconBg.source="source://image/order/wc.png";
		}
		/*收货地址*/
		if(null!=delivery){
			do_Label_consignee.text = "收货人："+delivery.consignee;
			do_Label_phone.text = delivery.phone;
			do_Label_address.text = "收货地址："+delivery.address;
		}
		button_phone.tag = data.buyerPhone;
		// 家政订单 加服务时间
		if (order.orderType == 1) {
			do_LinearLayout_1.add("serviceTime","source://view/order/homemaking/serviceTime.ui","do_ALayout_address");
			var serviceTime = ui("serviceTime");
			serviceTime.bindData(hashdata);
			if(null!=data.homemakingOrder){
				hashdata.addData({
					"serviceTimeStr" : data.homemakingOrder.serviceDateStr + " " + data.homemakingOrder.serviceHourStr
				});
			}
			serviceTime.refreshData();
		} else if (order.orderType == 5) { //代收代送
			var deliveryOrder = data.deliveryOrder;
			var deliveryNoUi = do_LinearLayout_1.add("serviceTime","source://view/order/homemaking/serviceTime.ui","do_ALayout_address");
			ui(deliveryNoUi+".do_Label_1").text = '物流号：';
			var serviceTime = ui("serviceTime");
			serviceTime.bindData(hashdata);
			hashdata.addData({
				"serviceTimeStr" : deliveryOrder.deliveryName +' '+deliveryOrder.deliveryNo
			});
			serviceTime.refreshData();
		}
		/*订单商品*/
		for(var i=0;i<itemList.length;i++) {
			var addItem = do_LinearLayout_item.add("item"+i, "source://view/order/detailTemplate.ui");
			var orderItemUi = ui("item"+i);
			orderItemUi.bindData(hashdata);
			hashdata.addData({
				"orderType" : order.orderType,
			    "productId" : itemList[i].productId,
			    "productName" :itemList[i].name + " " + itemList[i].productSpecName,
			    "productPrice" : "￥" + itemList[i].price,
			    "productCount" : itemList[i].quntity,
			    "productImg" : itemList[i].pathImg
			});
			orderItemUi.refreshData();
		}
		do_Label_buyer.text = data.buyerName;
		do_Label_total.text = "￥" + order.total;
		do_Label_orderNo.text = "订单号："+order.orderNo;
		do_Label_strNewAt.text = "下单时间："+order.strNewAt;
		if (order.payStatus == 2) {
			do_Label_payStatusAt.visible = true;
			do_Label_payStatusAt.text = "付款时间："+order.strPayStatusAt; // 付款时间
		} else if(order.payStatus == 4){
			do_Label_payStatusAt.visible = true;
			do_Label_payStatusAt.text = "退款时间："+order.strPayStatusAt;
		}
		if (order.payStatus != 1) { // 支付状态
			if(order.deliveryStatus == 1){
				if(order.orderType == 4){
					do_Button_sended.text="确认收款";
				}else{
					do_Button_sended.text="已发货";
				}
				do_Button_sended.visible=true;
				layoutBtn.visible=true;
			}
		}
		if (order.orderStatus == 2) {
			if (order.strOrderStatusAt) {
				do_Label_orderStatusAt.visible = true;
			}
			do_Label_orderStatusAt.text = "完成时间："+order.strOrderStatusAt; // 完成时间
		}
		if (order.payType == 1) {
			label_payTypeStr.text = "货到付款";
		} else {
			label_payTypeStr.text = "在线支付";
		}
		ui("do_LinearLayout_2").redraw();
	});
}
do_Button_sended.on("touch", function(data){
	nf.confirm("确认?", "", "确定", "取消", function(data, e) {
		if (data == 1) {
			if(order.orderType == 4){
				helperOrder.sellerSended(order.orderNo,'2',null,null,'1',function(data,e){
					do_Button_sended.visible=false;
				});
			}else if(order.orderType == 3){
				helperOrder.sellerSended(order.orderNo,'2',null,null,'1',function(data,e){
					do_Button_sended.visible=false;
				});
			}
		}
	});
});
button_phone.on("touch", function(data){
	if (button_phone.tag) {
		do_External.openDial(button_phone.tag);
	}
});